<?
namespace xto\data\dao;

use xto\App;
use xto\Util;
use xto\membership\core\UserRole;
use xto\data\IDao;
use xto\data\Sql;
use think\Db;

class MessageDao extends IDao{
	protected $table='xto_messages';
	protected $pk="mid";  
 

	public function save($data=[], $where = [], $sequence = NULL){
		$newdata=[
			'appid' 	=>$this->app->appid,
			'title' 	=>$data['title'],
			'content' 	=>$data['content'],
			'createdate'=>Util::getdate() 
		];
		$status = Sql::insert('xto_message_data',$newdata);
		if ($status) {
			$messages=[
				'mid'=>Db::table('xto_message_data')->getLastInsID(),
				'sender'=>$data['sender'],
				'accepter'=>$data['accepter'],
				'isread'=>$data['isread'], 
			];
			return parent::save($messages);
		}
		return $status;
	}

	public function get_messager($pagesize,$sender='',$accepter=''){
		if(!empty($sender)){
			$this->map['sender']=$sender;
		}
		if(!empty($accepter)){
			$this->map['accepter']=$accepter;
		}
		$result = Db::view('xto_messages a','*')
			->view('xto_message_data b','*','b.mid=a.mid') 
			->order($this->order)
			->where($this->map)
		    ->paginate($pagesize);
		return $result;
	}

	public function find($mbid){
		$this->map['mbid']=$mbid;
		$result = Db::view('xto_messages a','*')
			->view('xto_message_data b','*','b.mid=a.mid') 
			->where($this->map)
			->find();
		return $result;
	}
}